<?php

class Offer_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	//Insert a new offer
	function insert_offer($offer) {		
		$sql = "INSERT INTO offer (item_name, item_price, item_condition, age_year, age_month, item_description, item_pic, fb_id, category_id, sub_category_id, status, date_created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";		
		$result = $this->db->query($sql, array(strip_tags($offer['item_name']), $offer['item_price'], $offer['item_condition'], $offer['age_year'], $offer['age_month'], strip_tags($offer['item_description']), $offer['item_pic'], $offer['fb_id'], $offer['category_id'], $offer['sub_category_id'], OFFER_NEW));
		
		if($offer['item_pic'] != 'default.png'){
			$offer_id = $this->db->insert_id();
			$path = pathinfo($offer['item_pic']);
			$this->update_image_name($offer_id, $offer['item_pic'], 'item_' . $offer_id . '.' . $path['extension']);
		}

		return $result;
	}
	
	//Update a new offer
	function update_offer($offer, $offer_id) {		
		$sql = "UPDATE offer SET item_name = ?, item_price = ?, item_condition = ?, age_year = ?, age_month = ?, item_description = ?, item_pic = ?, category_id = ?, sub_category_id = ? WHERE offer_id = ?";		
		$result = $this->db->query($sql, array(strip_tags($offer['item_name']), $offer['item_price'], $offer['item_condition'], $offer['age_year'], $offer['age_month'], strip_tags($offer['item_description']), $offer['item_pic'], $offer['category_id'], $offer['sub_category_id'], $offer_id));
		
		$path = pathinfo($offer['item_pic']);
		if($offer['item_pic'] == 'temp_'. $offer['fb_id'] . '.' . $path['extension']){			
			$this->update_image_name($offer_id, $offer['item_pic'], 'item_' . $offer_id . '.' . $path['extension']);
		}
		return $result;
	}

	//Retrieve an offer based on the offer id
	function get_offer_by_offer_id($offer_id) {
		$sql = "SELECT * FROM offer WHERE offer_id = ?";
		$query = $this->db->query($sql, $offer_id);
			
		$data = array();
		if($query->num_rows() > 0){
			$data = $query->row_array();				
		}
		return $data;
	}
	
	//Retrieve offers based on the fb id
	function get_offers_by_fb_id($fb_id) {
		$sql = "SELECT * FROM offer WHERE fb_id = ?";
		$query = $this->db->query($sql, $fb_id);
			
		$data = array();
		if($query->num_rows() > 0){
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}		
		}
		return $data;
	}
	
	//Retrieve latest new offers
	function get_latest_offers($num){
		$sql = "SELECT * FROM offer WHERE status != ? ORDER BY date_created DESC LIMIT ?";
		$query = $this->db->query($sql, array(OFFER_CLOSED, $num));
		
		$data = array();
		if($query->num_rows() > 0){
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}		
		}
		return $data;
	}
	
	
	//Update image name based on the offer id (should not be called by external functions)
	private function update_image_name($offer_id, $old_name, $new_name){
		$sql = "UPDATE offer SET item_pic = ? WHERE offer_id = ?";
		$result = $this->db->query($sql, array($new_name, $offer_id));

		rename(getcwd()."/resources/item_images/".$old_name, getcwd()."/resources/item_images/".$new_name);
		return $result;
	}
}
?>